package core

import (
	"context"
)

func NewAgent(task *TraceTask) (Agent, error) {
	ctx, cancelFunc := context.WithCancel(context.Background())

	stat := NewTraceStat(task.MaxTTL)
	aggregator := NewAggregator(ctx, task, stat)
	printer := NewPrinter(ctx, task, stat)

	var sender Sender
	var receiver Receiver
	if task.Proto == "icmp" {
		sender = NewICMPSender(ctx, task, stat)
		receiver = NewICMPReceiver(ctx, task, stat)
	} else if task.Proto == "udp" {
		sender = NewUDPSender(ctx, task, stat)
		receiver = NewUDPReceiver(ctx, task, stat)
	} else if task.Proto == "tcp" {
		sender = NewTCPSYNSender(ctx, task, stat)
		receiver = NewTCPSYNReceiver(ctx, task, stat)
	}

	agent := &BaseAgent{
		Name:       "Agent",
		Sender:     sender,
		Receiver:   receiver,
		Aggregator: aggregator,
		Printer:    printer,

		Ctx:        ctx,
		CancelFunc: cancelFunc,
	}

	return agent, nil
}
